using System;
using System.Collections;
using System.Collections.Generic;
using DDA;
using Sirenix.OdinInspector;
using Survivor;
using Unity.VisualScripting.Dependencies.NCalc;
using UnityEngine;

public abstract class DDACondition : DDATreeNodeBase
{
    public override int NextNodeMaxNum => 0;
    public override bool Invoke()
    {
        base.Invoke();
        return true;
    }
}

public class DDABool : DDACondition
{
    [ShowInInspector, LabelText("bool值")] public bool isOk;
    public override bool Invoke()
    {
        base.Invoke();
        return isOk;
    }
}

public class DDAGameTimeCheck : DDACondition,IInputObject
{
    public override string NodeName
    {
        get
        {
            var time = Mathf.RoundToInt(initTime + invokeGameTime);
            var seconds = time % 60;
            var minutes = time / 60;
            return $"{minutes:00}:{seconds:00}";
        }
    }
    [ShowInInspector, LabelText("初始时间")] public float initTime;
    [ShowInInspector, LabelText("触发时间")] public float invokeGameTime = 999;
    public override bool Invoke()
    {
        base.Invoke();
        if (Global.CurrentTime.Value >=initTime+invokeGameTime)
        {
#if UNITY_EDITOR
            Debug.Log($"DDAGameTimeCheck:触发时间：{invokeGameTime} 检测成功");
#endif
            return true;
        }
        return false;
    }

    public Type InputType()
    {
        return typeof(float);
    }

    public void ReceiveObject(object obj)
    {
        this.initTime = (float)(obj);
    }
}

#region 判断能力解锁

public class DDAAbilityLevelChecker : DDACondition,IInputObject
{
    [ShowInInspector, LabelText("能力")] public AbilityEnum abilityEnum;
    [ShowInInspector, LabelText("目标等级"),Tooltip("能力达到目标等级时返回true")] public float invokeLevel = 0;
    public override bool Invoke()
    {
        base.Invoke();
        var system = Global.Interface.GetSystem<ExpUpgradeSystem>();
        if (system != null && system.items.Count>0)
        {
            return system.items[(int)abilityEnum].Level > invokeLevel;
        }

        return true;
    }

    public Type InputType()
    {
        return typeof(float);
    }
    

    public void ReceiveObject(object obj)
    {
        this.invokeLevel =Mathf.RoundToInt((float)obj);
    }
}

#endregion

public class DDAIfWithConditionSequence : DDAControlFlow
{
    public override int NextNodeMaxNum => 3;
    public override bool Invoke()
    {
        base.Invoke();
        if (ChildNodes.Count < 2)
        {
#if UNITY_EDITOR
            Debug.LogWarning("DDAIfElse节点不能小于2个");
#endif
            return true;
        }
        if (ChildNodes[0] is DDAConditionSequence ddaConditionSequence)
        {
            if (ddaConditionSequence.isOver) return true;
            if (ddaConditionSequence.Invoke())
            {
                    ChildNodes[1] .Invoke();
            }
            else
            {
                if (ChildNodes.Count >= 3)
                {
                    ChildNodes[2].Invoke();
                }
            }
        }
        else
        {
#if UNITY_EDITOR
            Debug.LogWarning("DDAIfWithConditionSequence 必须配合  DDAConditionSequence 使用");
#endif
        }


        return false;
    }
}

public class DDAConditionSequence : DDAControlFlow
{
    public override int NextNodeMaxNum => 10;
    [ShowInInspector, ReadOnly] private int _index = 0;
    [ShowInInspector,LabelText("序列中全部条件判断完成")] public bool isOver = false;

    public override bool Invoke()
    {
        base.Invoke();
        if (_index >= ChildNodes.Count)
        {
            isOver = true;
            return true;
        }
        var result = ChildNodes[_index].Invoke();
        if (result) _index += 1;
        return result;
    }

    public override void Reset()
    {
        base.Reset();
        _index = 0;
        isOver = false;
    }
}

